package com.zh.juc.atomic;

import java.util.concurrent.atomic.LongAccumulator;
import java.util.concurrent.atomic.LongAdder;

/**
 * @Author: zhanghuan
 * @date: 2022/10/22 09:14
 * @description: create it
 * LongAdder:
 * - 只能用来计算加法，从零开始
 * LongAccumulator：
 * - 提供自定义函数操作
 * <p>
 * 热点商品点赞计算器，点赞数加加统计，不要求实时精确
 */
public class LongAdderAPIDemo {
    public static void main(String[] args) {
        LongAdder longAdder = new LongAdder();
        longAdder.increment();
        longAdder.increment();
        longAdder.increment();
        longAdder.increment();

        System.out.println(longAdder.sum());//4

        LongAccumulator longAccumulator = new LongAccumulator((x, y) -> x + y, 0);
        longAccumulator.accumulate(1);//1
        longAccumulator.accumulate(3);//4
        System.out.println(longAccumulator.get());
    }
}
